package com.kingtone.rongji.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JsonUtils {

	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");

	public static String convertToJsonDateTime(String javaDate) {
		if (javaDate == null || javaDate.length() == 0) {
			return null;
		}
		Date currentDate = null;
		try {
			currentDate = dateFormat.parse(javaDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time = currentDate.getTime();
		return "/Date(" + time + "+0800)/";
	}

	public static String convertToStringDateTime(String jsonDate) {
		if (jsonDate == null || jsonDate.length() == 0) {
			return "";
		}
		int first = jsonDate.indexOf("/Date(");
		int last = jsonDate.indexOf("+0800");
		if (first >= 0 && last > 0) {
			long time = Long.parseLong(jsonDate.substring(first + 6, last));
			Date date = new Date(time);
			return dateFormat.format(date);
		}
		return "";
	}
}
